import axios from 'axios'

const API_URL = 'http://localhost:8080/api/auth/'

// 配置axios默认设置
axios.defaults.withCredentials = true;

class AuthService {
  async login(credentials) {
    try {
      const response = await axios.post(API_URL + 'login', credentials)
      return response.data
    } catch (error) {
      throw this.handleError(error)
    }
  }

  async register(user) {
    try {
      const response = await axios.post(API_URL + 'register', user)
      return response.data
    } catch (error) {
      throw this.handleError(error)
    }
  }

  handleError(error) {
    if (error.response && error.response.data) {
      return {
        message: error.response.data.message || '操作失败',
        status: error.response.status
      }
    }
    return {
      message: error.message || '网络错误',
      status: 500
    }
  }

  logout() {
    localStorage.removeItem('token')
  }
}

export default new AuthService() 